﻿using UnityEngine;
using System.Collections;

public class ObjFactory 
{
    //gameObj
      private static GameObject m_pBullet      =  Resources.Load("Prefaps/Effect/Bullet") as GameObject;
      private static GameObject m_pShell = Resources.Load("Prefaps/Props/Shell") as GameObject;
      private static GameObject m_pMagazine = Resources.Load("Prefaps/Props/Magazine") as GameObject;
      private static GameObject m_pBulletFire = Resources.Load("Prefaps/Effect/GunFire") as GameObject;
      private static GameObject m_pBulletSmoke = Resources.Load("Prefaps/Effect/GunFireSmoke") as GameObject;
      private static GameObject m_pStone = Resources.Load("Prefaps/Effect/Stone_0") as GameObject;

    //Effect 
      public static void CreateBullet(Vector3 vecPos, Vector3 vecDest, bool bFlag, GameObject pOwner, string strTargetTag)
    {

        (GameObject.Instantiate(m_pShell, vecPos, Calculate.RotToAngle(Random.Range(0.0f, 360.0f) + 90.0f)) as GameObject).transform.parent = GameObject.Find("Effect").transform;

        Vector3 FinDest = vecDest;
        FinDest = Vector3.Normalize(FinDest - vecPos);


        Vector3 vecFire = vecPos + (FinDest * 0.17f);
        vecPos = vecPos + (FinDest * 0.3f);

        GameObject pBullet = GameObject.Instantiate(m_pBullet, vecPos, Quaternion.identity) as GameObject;
        pBullet.transform.parent = GameObject.Find("Effect").transform;
        Calculate.RotToDest(pBullet, vecDest);
        pBullet.GetComponent<Bullet>().m_vecDest = vecDest;
        pBullet.GetComponent<Bullet>().m_bHitFlag = bFlag;
        pBullet.GetComponent<Bullet>().m_pOwner = pOwner;
        pBullet.GetComponent<Bullet>().m_strTargettag = strTargetTag;

        GameObject pBulletFire;
        if(pOwner.GetComponent<UnitStatus>().m_WeaponData.m_strWeaponName == "FN303")
            pBulletFire = GameObject.Instantiate(m_pBulletSmoke, vecFire, Quaternion.identity) as GameObject;
        else
            pBulletFire = GameObject.Instantiate(m_pBulletFire, vecFire, Quaternion.identity) as GameObject;

        Calculate.RotToDest(pBulletFire, vecDest);
        pBulletFire.transform.parent = GameObject.Find("Effect").transform;
    }

    public static void CreateThrow(Vector3 vecPos, Vector3 vecDest, bool bFlag)
    {

        Vector3 FinDest = vecDest;
        FinDest = Vector3.Normalize(FinDest - vecPos);


        Vector3 vecFire = vecPos + (FinDest * 0.17f);
        vecPos = vecPos + (FinDest * 0.3f);

        GameObject pStone = GameObject.Instantiate(m_pStone, vecPos, Quaternion.identity) as GameObject;
        Calculate.RotToDest(pStone, vecDest);
        pStone.GetComponent<Throw>().m_strTargettag = "Player";
        pStone.GetComponent<Throw>().m_vecDest = vecDest;
        pStone.GetComponent<Throw>().m_bHitFlag = bFlag;

    }

    //Props
    public static void CreateMagazine(Vector3 vecPos)
    {
        int iRandPosFlag = Random.Range(0, 1);
        float fPosX = 0.1f;

        if (iRandPosFlag == 0)
            fPosX = -0.1f;

        GameObject.Instantiate(m_pMagazine, new Vector2(vecPos.x + fPosX, vecPos.y),
              Calculate.RotToAngle(Random.Range(0.0f, 360.0f)));
    }


    public static void AddPerk(GameObject objTarget, int iPerkID)
    {
        objTarget.GetComponent<UnitStatus>().m_unitData.ListPerk.Add(GameObject.Find("DataLogic").GetComponent<DataManager>().m_ListPerkData[iPerkID]);
        ResourceMgr.GetInstance().CreateInGameText(objTarget, GameObject.Find("DataLogic").GetComponent<DataManager>().m_ListPerkData[iPerkID].m_strName, new Color(58/255f, 39/255f, 24/255f), "Skull");
    } 
}
